package com.gylang.im.core.myassert;

import com.gylang.im.core.exception.BaseException;
import com.gylang.im.core.exception.BizException;
import com.gylang.im.core.exception.ValidateException;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @author gylang,
 * date 2020/5/2,
 * @version 1.0
 */
@Slf4j
public class MyAssert implements BaseAssert {

    @Override
    public void valid(String code, String msg) {

        throw new ValidateException(code, msg);
    }

    @Override
    public void biz(String code, String msg) {

        throw new BizException(code, msg);
    }

    @Override
    public <T extends BaseException> void other(String code, String msg, Class<T> t) {

        try {
            Constructor<T> constructor = t.getDeclaredConstructor(String.class, String.class);
            if (null != constructor) {
                throw constructor.newInstance(code, msg);

            }
        } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
            log.info("#MyAssert.otherException 抛出异常错误 = {}", e.getMessage());
        }
    }
}
